#!/bin/bash
# 数组
# characters=("hidden")
characters="lp1"

# 遍历 /dev/usb/ 目录中的所有文件
for file in /dev/usb/*; do
    base_file=$(basename "$file")
    # echo "base_file:$base_file"
    # if [[ "$base_file" != "${characters[0]}" ]]; then
    if [ $base_file == $characters ]; then
        echo "Opening device: $base_file"
        nL=124  # 用您需要的具体数值替换
        nH=0   # 用您需要的具体数值替换

        #左对齐的指令集
        echo -ne '\x1B\x61\x00' > "$file"    
        #设置字体和字体大小
        echo -ne '\x1B\x4D\x00' > "$file"
        echo -ne '\x1D\x21\x01' > "$file"

        #打印抬头
        print_head=${19}
        printf " %s\n" "$print_head" | iconv -f UTF-8 -t GBK >> "$file"


        # 发送 ESC/P 指令
        # 中间对齐的指令集
        echo -ne '\x1B\x61\x01' > "$file"
        # 设置字体和字体大小
        echo -ne '\x1B\x4D\x00' > "$file"
        echo -ne '\x1D\x21\x01' > "$file"

        # 发送文本内容
        string="测试结果"
        printf "      %s\n" "$string" | iconv -f UTF-8 -t GBK >> "$file"

        # 初始化打印机
        echo -ne '\x1B\x40' > "$file"

        string3="日差"
        string4="偏振"
        string5="摆幅"
        printf "             %4s  %4s  %4s\n" "$string3" "$string4" "$string5" | iconv -f UTF-8 -t GBK >> "$file"
        
        # 左对齐
        echo -ne '\x1B\x61\x00' > "$file"
        # 结果数据
        printf "P000      :  %-4s  %-4s  %-3s\n" "$1" "$2" "$(echo "${3}/1" | bc)" >> "$file"
        printf "P090      :  %-4s  %-4s  %-3s\n" "$4" "$5" "$(echo "${6}/1" | bc)" >> "$file"
        printf "P180      :  %-4s  %-4s  %-3s\n" "$7" "$8" "$(echo "${9}/1" | bc)" >> "$file"
        printf "P270      :  %-4s  %-4s  %-3s\n" "${10}" "${11}" "$(echo "${12}/1" | bc)" >> "$file"

        # 设置字体和字体大小  
        echo -ne '\x1B\x4D\x00' > "$file"
        echo -ne '\x1D\x21\x00' > "$file"

        string_Xv="瞬时平均值"
        string_Xp="总平均值  "
        printf "%5s: " "$string_Xv" | iconv -f UTF-8 -t GBK >> "$file"
        printf " %-3s  %-4.1f  %-3s\n" "${13}" "${14}" "$(echo "${15}/1" | bc)" >> "$file"

        printf "%5s: " "$string_Xp" | iconv -f UTF-8 -t GBK >> "$file"
        printf " %-3s  %-4.1f  %-3s\n" "${16}" "${17}" "$(echo "${18}/1" | bc)" >> "$file"

        # 添加空行
        printf "\n" >> "$file"
        printf "\n" >> "$file"
        printf "\n" >> "$file"
    fi
done

# 蓝牙USB串口
if [ -e /dev/ttyACM0 ]; then
    owner_stat=$(stat -c '%U' /dev/ttyACM0)
    if [ "$owner_stat" == "bj" ]; then
        echo "The owner of /dev/ttyACM0 is bj (checked with stat)."
    else
        echo "The owner of /dev/ttyACM0 is not bj (checked with stat). It is $owner_stat."
        echo 1 | sudo -S chown bj /dev/ttyACM0
    fi

    file="/dev/ttyACM0"

    #左对齐的指令集
    echo -ne '\x1B\x61\x00' > "$file"    
    #设置字体和字体大小
    echo -ne '\x1B\x4D\x00' > "$file"
    echo -ne '\x1D\x21\x01' > "$file"

    #打印抬头
    print_head=${19}
    printf " %s\n" "$print_head" | iconv -f UTF-8 -t GBK >> "$file"


    # 发送 ESC/P 指令
    # 中间对齐的指令集
    echo -ne '\x1B\x61\x01' > "$file"
    # 设置字体和字体大小
    echo -ne '\x1B\x4D\x00' > "$file"
    echo -ne '\x1D\x21\x01' > "$file"

    # 发送文本内容
    string="测试结果"
    printf "      %s\n" "$string" | iconv -f UTF-8 -t GBK >> "$file"

    # 初始化打印机
    echo -ne '\x1B\x40' > "$file"

    string3="日差"
    string4="偏振"
    string5="摆幅"
    printf "             %4s  %4s  %4s\n" "$string3" "$string4" "$string5" | iconv -f UTF-8 -t GBK >> "$file"
    
    # 左对齐
    echo -ne '\x1B\x61\x00' > "$file"
    # 结果数据
    printf "P000      :  %-4s  %-4s  %-3s\n" "$1" "$2" "$(echo "${3}/1" | bc)" >> "$file"
    printf "P090      :  %-4s  %-4s  %-3s\n" "$4" "$5" "$(echo "${6}/1" | bc)" >> "$file"
    printf "P180      :  %-4s  %-4s  %-3s\n" "$7" "$8" "$(echo "${9}/1" | bc)" >> "$file"
    printf "P270      :  %-4s  %-4s  %-3s\n" "${10}" "${11}" "$(echo "${12}/1" | bc)" >> "$file"

    # 设置字体和字体大小  
    echo -ne '\x1B\x4D\x00' > "$file"
    echo -ne '\x1D\x21\x00' > "$file"

    string_Xv="瞬时平均值"
    string_Xp="总平均值  "
    printf "%5s: " "$string_Xv" | iconv -f UTF-8 -t GBK >> "$file"
    printf " %-3s  %-4.1f  %-3s\n" "${13}" "${14}" "$(echo "${15}/1" | bc)" >> "$file"

    printf "%5s: " "$string_Xp" | iconv -f UTF-8 -t GBK >> "$file"
    printf " %-3s  %-4.1f  %-3s\n" "${16}" "${17}" "$(echo "${18}/1" | bc)" >> "$file"

    # 添加空行
    printf "\n" >> "$file"
    printf "\n" >> "$file"
    printf "\n" >> "$file"


else
    echo "/dev/ttyACM0 does not exist (checked with test)" 
fi
